//
//  PTDemandViewController.swift
//  PropertyClient
//
//  Created by benwang on 2021/6/22.
//

import UIKit
import CollectionKit

class PTDemandViewController: UIViewController {

  var collectionView = CollectionView()
  let locationButtonItem = LocationButtonItem(maxLength: 12)
  let sheetHandler = PTSheetHandle()

  override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    navigationItem.leftBarButtonItem = locationButtonItem
    locationButtonItem.text = "正在定位中..."

    opacityNavigationBar(color: UIColor(hex: 0xFAFAFA))

    view.backgroundColor = UIColor(hex: 0xFAFAFA)
    view.addSubview(collectionView)

    collectionView.snp.makeConstraints { maker in
      maker.edges.equalToSuperview()
    }

    let dataSource = ArrayDataSource<DemandItem>(
      data: DemandItem.items
    )

    let viewSource = ClosureViewSource<DemandItem, UnitImageTextView> { item, data, index in
      item.image = UIImage(named: data.icon)
      item.text = data.rawValue
    }

    let sizeSource = { (index: Int, data: DemandItem, size: CGSize) in
      return CGSize(width: size.width - 30, height: 80)
    }

    let inset = UIEdgeInsets(top: 10, left: 0, bottom: 0, right: 0)

    let itemProvider = BasicProvider<DemandItem, UnitImageTextView>(
      dataSource: dataSource,
      viewSource: viewSource,
      sizeSource: sizeSource,
      layout: FlowLayout(spacing: 12, justifyContent: .center)
        .inset(by: inset)
    )

    itemProvider.animator = FadeAnimator()
    itemProvider.tapHandler = { (context) in
      self.jump(to: context.data)
    }

    collectionView.provider = ComposedProvider(sections: [itemProvider])

    DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
      self.locationButtonItem.text = "老北京四合院"
    }
  }

  func jump(to item: DemandItem) {
    let vc = DemandNoteViewController()
    vc.title = item.rawValue
    vc.hidesBottomBarWhenPushed = true
    navigationController?.pushViewController(vc, animated: true)
  }
}

enum DemandItem: String {
  case wyjf = "物业缴费"
  case gnjf = "供暖缴费"
  case cwjf = "车位缴费"

  var icon: String {
    switch self {
    case .wyjf:
      return "icon_wyjf"
    case .gnjf:
      return "icon_gnjf"
    case .cwjf:
      return "icon_cwjf"
    }
  }

  static let items: [DemandItem] = [
    .wyjf, .gnjf, .cwjf
  ]
}
